<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\index\BaseController;
use app\index\logic\Token;
use app\index\model\Category;
use app\index\model\Product;
use scws\PSCWS4;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Cate extends BaseController
{
    //分类页
    public function category()
    {
        if (isset($_GET['code']) && !empty($_GET['code'])){
            Token::token($_GET['code']);
        }
        $list = Category::where('parentid','=','0')
            ->order('sort','asc')
            ->select();
        View::assign('list',$list);
        $get = Request::get();
        if (empty($get['id']) || $get['id']=='' || !isset($get['id'])){
            $get['id'] = 20;
        }
        View::assign('get',$get['id']);
        $line = Category::where('parentid','=',$get['id'])->select();
        View::assign('line',$line);
        return View::fetch();
    }

    //分类列表
    public function list()
    {
//        if(Session::get('user')==null){
//            header("Location:category");
//        }
        $get = Request::get();
        $all = Request::param();
        $where=[];
        $whereOr=[];
//        $where_attr=[];
        //分词
        if (isset($get['key']) && !empty($get['key'])){
            new PSCWS4();
            $psc = new PSCWS4();
            $psc->set_charset("utf8");
            $psc->set_dict(ROOT_PATH.'/extend/scws/etc/dict.utf8.xdb');//加载词库
            $psc->set_rule(ROOT_PATH.'/extend/scws/etc/rules.ini');//加载配置文件
            $psc->send_text($get['key']);
            $tops = $psc->get_tops(88, 'n,v');
            if($tops){
                foreach ($tops as $k=>$v){
                    $whereOr[] = ['p.name','like','%'.$v['word'].'%'];
                }
            }
        }else{
            $get['key']="";
        }
        //常规进入
        if (isset($get['id']) && !empty($get['id'])){
            $where[] = ['p.cid','=',$get['id']];
        }else{
            $get['id']="";
        }
        //属性筛选
        //建立新数组
        $arr = [];
        $attr = Product::suffix('_attr')
            ->alias('pa')
            ->field('distinct a.attr_id as id,a.attr_name as type,pa.attr_value as value')
            ->leftJoin('product p','p.id=pa.goods_id')
            ->leftJoin('attribute a','a.attr_id=pa.attr_id')
            ->where($where)
            ->whereOr($whereOr)
            ->select();
        foreach ($attr as $k=>$v){
           $arr[$v['type']][]=['id'=>$v['id'],'value'=>$v['value']];
        }
        //添加url路径
        foreach ($arr as $k=>$v) {
            foreach ($v as $kk => $vv) {
                $url_attr = $all;//接到的所有参数给$url
                $url_attr["attr_" . $vv['id']] = $vv['value'];
                $arr[$k][$kk]['url'] = url('cate/list', $url_attr);
            }
            //添加全部的属性
            //清除接到的所有参数中此类属性的值
            $url_attr2 = $all;
            unset($url_attr2['attr_'.$v[0]['id']]);
            $all_url = [
                'id' => $v[0]['id'],
                'value' => '全部',
                'url' => url('cate/list',$url_attr2)
            ];
            //全部要放到所有的属性之前
            array_unshift($arr[$k], $all_url);
        }
        $where_attr=[];
        $attr_where=[];
        //过滤掉我们需要的属性
        foreach ($all as $k=>$v){
            if(substr($k,0,5)=="attr_"){
                $attr_where[substr($k,5)]=$v;

            }
        }
        foreach ($attr_where as $k=>$v){
            $where_attr[]=['pa.attr_id','=',$k];
            $where_attr[]=['pa.attr_value','=',$v];//"(pa.attr_id = {$k} AND pa.attr_value = '{$v}') or ";
        }
        //展示
        $str = Product::alias('p')
//            ->field('p.id,p.name,p.photo_x,p.price_shop')
            ->leftJoin('product_attr pa','pa.goods_id=p.id')
            ->where($where)
            ->where(function ($query) use($whereOr) {
                    $query->whereOr($whereOr);
            })
            ->where(function ($query) use($where_attr) {
                    $query->whereOr($where_attr);
            })
//            ->whereOr($where_attr)
//            ->whereOr($whereOr)
            ->group('p.id')
            ->select();
        if (isset($str) && !empty($str)){
            $str=$str->toArray();
            View::assign('str',$str);
        }
        View::assign('attr',$arr);
        View::assign('cid',$get['id']);
        View::assign('key',$get['key']);
        View::assign('all',$all);
        return View::fetch();
    }


}